package com.ff.discounts.component.saleorder

import android.content.Context
import androidx.viewpager2.widget.ViewPager2
import com.ff.discounts.R
import com.ff.discounts.activity.BaseTitleActivity
import com.ff.discounts.databinding.ActivitySaleOrderBinding
import com.ff.discounts.util.Constant
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
* 特卖订单
*/
class SaleOrderActivity : BaseTitleActivity<ActivitySaleOrderBinding>() {

    private lateinit var adapter: SaleOrderPageAdapter

    override fun initViews() {
        super.initViews()
        binding.list.offscreenPageLimit = 2
    }

    override fun initDatum() {
        super.initDatum()

        adapter = SaleOrderPageAdapter(hostActivity)
        binding.list.adapter = adapter
        adapter.setDatum(
            listOf(
                Constant.NUMBER_ONE,
                Constant.NUMBER_ONE
            )
        )

        //创建通用指示器
        val commonNavigator = CommonNavigator(hostActivity)
        // 设置适配器
        commonNavigator.adapter = object :CommonNavigatorAdapter(){
            /**
            * 指示器数量
            */

            override fun getCount(): Int {
                return indicatorTitles.size
            }
            /**
            * 返回当前位置的标题
            */
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                // 创建简单的文本控件
                val titleView = SimplePagerTitleView(context)
                //默认颜色
                titleView.normalColor = getColor(R.color.black80)
                //选中后的颜色
                titleView.selectedColor = getColor(R.color.primary)
                //设置选中文本
                titleView.setText(indicatorTitles[index])
                // 点击回调监听
                titleView.setOnClickListener {
                    binding.list.currentItem = index
                }

                return titleView
            }

            /**
            * 返回指示器
            */
            override fun getIndicator(context: Context?): IPagerIndicator {
                // 创建一条线
                val indicator = LinePagerIndicator(context)
                // 线的宽度和内容一样
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                //高亮颜色
                indicator.setColors(getColor(R.color.primary))
                return indicator
            }

        }

        //是否自动调整
        commonNavigator.isAdjustMode = true
        // 设置导航器
        binding.indicator.navigator = commonNavigator
        binding.list.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                binding.indicator.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.indicator.onPageSelected(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                binding.indicator.onPageScrollStateChanged(state)
            }
        })

    }

    companion object{
        private val indicatorTitles = intArrayOf(
            R.string.subsidy,
            R.string.shop_order
        )
    }
}